package com.aididi;

import com.aididi.factory.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RabbitmqMessageQueueApplication {
	//交换机名称
	static final String exchangeName = "my-exchange";
	//RoutingKey,用于将Queue Binding到一个Exchange上
	static final String queryName = "my-routekey";

	@Bean
	Queue queue(){
		return new Queue(queryName,false);
	}

	@Bean
	TopicExchange exchange(){
		return new TopicExchange(exchangeName);
	}

	@Bean
	Binding binding(Queue queue,TopicExchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
	}

	@Bean
	SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
											 MessageListenerAdapter adapter){
		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.setQueueNames(queryName);
		container.setMessageListener(adapter);
		return container;
	}

	@Bean
	MessageListenerAdapter adapter(Receiver receiver){
		return new MessageListenerAdapter(receiver,"receiverMessage");
	}


	public static void main(String[] args) {
		SpringApplication.run(RabbitmqMessageQueueApplication.class, args).close();
	}

}
